package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnDeliveryAddress;
import com.example.zhunongzonghe.service.IZnDeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;


@RestController
@RequestMapping("/znDeliveryAddress")
public class ZnDeliveryAddressController {
    @Autowired
    private IZnDeliveryAddressService znDeliveryAddressService;

    /**
     * 当前用户所有地址
     *
     * @param principal
     * @return
     */
    @GetMapping("/addressAll")
    public Result<List<ZnDeliveryAddress>> addressAll(Principal principal) {
        LambdaQueryWrapper<ZnDeliveryAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnDeliveryAddress::getUsername, principal.getName());
        List<ZnDeliveryAddress> list = znDeliveryAddressService.list(wrapper);
        return new Result<>(true, "查询成功", 200, list);
    }

    /**
     * 当前用户默认地址
     *
     * @param principal
     * @return
     */
    @GetMapping("/defaultAddress")
    public Result<ZnDeliveryAddress> defaultAddress(Principal principal) {
        System.out.println("-----------"+principal.getName());
        LambdaQueryWrapper<ZnDeliveryAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnDeliveryAddress::getUsername, principal.getName()).eq(ZnDeliveryAddress::getDefaultAddress, 1);
        ZnDeliveryAddress znDeliveryAddress = znDeliveryAddressService.getOne(wrapper);
        System.out.println("值为："+znDeliveryAddress);
        return new Result<>(true, "查询成功", 200, znDeliveryAddress);
    }

    /**
     * 当前用户编辑地址
     *
     * @param znDeliveryAddress
     * @return
     */
    @PostMapping("/editAddress")
    public Result editAddress(@RequestBody ZnDeliveryAddress znDeliveryAddress,Principal principal) {
        System.out.println("-------------" + znDeliveryAddress);
        if (znDeliveryAddress.getDefaultAddress() == 1) {
            List<ZnDeliveryAddress> deliveryAddressList =
                    znDeliveryAddressService.queryUserNameAdderss(principal.getName());
            for (ZnDeliveryAddress Address : deliveryAddressList
            ) {
                Address.setDefaultAddress(0);
                znDeliveryAddressService.updateById(Address);
            }
        }
        znDeliveryAddressService.updateById(znDeliveryAddress);
        return new Result(true, "修改成功", 200);
    }

    /**
     * 当前用户添加地址
     *
     * @param znDeliveryAddress
     * @return
     */
    @PostMapping("/addUserAddress")
    public Result addUserAddress(@RequestBody ZnDeliveryAddress znDeliveryAddress,Principal principal) {
        znDeliveryAddress.setUsername(principal.getName());
        if (znDeliveryAddress.getDefaultAddress() == 1) {
            List<ZnDeliveryAddress> deliveryAddressList =
                    znDeliveryAddressService.queryUserNameAdderss(principal.getName());
            for (ZnDeliveryAddress Address : deliveryAddressList
            ) {
                Address.setDefaultAddress(0);
                znDeliveryAddressService.updateById(Address);
            }
        }
        znDeliveryAddressService.save(znDeliveryAddress);
        return new Result(true, "添加成功", 200);
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAddress/{id}")
    public Result deleteAddress(@PathVariable Integer id) {
        znDeliveryAddressService.removeById(id);
        return new Result(true, "删除成功", 200);
    }

    @PostMapping("/changeAddress")
    public Result changeAddress(@RequestParam("addressId") Integer id,Principal principal) {
        System.out.println("接收到的数据为-----：" + id);
        System.out.println("接收到的数据为-----：" + principal.getName());
        if (id == null || principal.getName()==null) return null;
        ZnDeliveryAddress znDeliveryAddress = znDeliveryAddressService.getById(id);
        znDeliveryAddress.setDefaultAddress(1);
        List<ZnDeliveryAddress> deliveryAddressList = znDeliveryAddressService.
                queryUserNameAdderss(principal.getName());
        System.out.println(deliveryAddressList);
        for (ZnDeliveryAddress Address : deliveryAddressList
        ) {
            Address.setDefaultAddress(0);
            znDeliveryAddressService.updateById(Address);
        }
        znDeliveryAddressService.updateById(znDeliveryAddress);
        return new Result(true, "更换成功", 200);
    }
}